package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.common.BaseContext;
import com.example.common.R;
import com.example.domain.AddressBook;
import com.example.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {


    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private RedisTemplate redisTemplate;



    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {

        addressBook.setUserId(BaseContext.getThreadLocal());

        addressBookService.save(addressBook);
        redisTemplate.delete("addressBook");

        return R.success(addressBook);
    }


    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {

        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getThreadLocal());
        wrapper.set(AddressBook::getIsDefault, 0);
        //SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(wrapper);

        addressBook.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);

        redisTemplate.delete("addressBook");
        return R.success(addressBook);
    }


    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {

        List<AddressBook> addressBook1 = (List<AddressBook>) redisTemplate.opsForValue().get("addressBook");

        if (addressBook1 !=null){
            return R.success(addressBook1);
        }

        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<AddressBook>();

        wrapper.eq( AddressBook::getUserId, BaseContext.getThreadLocal());

        wrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> list = addressBookService.list(wrapper);

        redisTemplate.opsForValue().set("addressBook", list,3600L, TimeUnit.SECONDS);

        return R.success(list);

    }


    @GetMapping("/default")
    public R<AddressBook> defaults() {

        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<AddressBook>();

        wrapper.eq(AddressBook::getUserId, BaseContext.getThreadLocal());

        wrapper.eq(AddressBook::getIsDefault, 1);

        AddressBook addressBook = addressBookService.getOne(wrapper);

        redisTemplate.delete("addressBook");

        return R.success(addressBook);

    }


    @GetMapping("/{id}")
    public R<AddressBook> set(@PathVariable Long id){

        AddressBook byId = addressBookService.getById(id);

        redisTemplate.delete("addressBook");

        return R.success(byId);
    }


    @PutMapping
    public R<String> setAddress(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);

        redisTemplate.delete("addressBook");

        return R.success("修改成功！");
    }

}
